package b1.mobile.blackberry.ui.component;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;

/**
 * 
 * @author YangShen
 *
 */
public class IconLabelField extends Field {
	private Bitmap bitmap;
	private String text;

	public IconLabelField(Bitmap bitmap, String text) {
		this.bitmap = bitmap;
		this.text = text;
	}

	protected void layout(int width, int height) {
		setExtent(width, this.bitmap.getHeight());
	}

	protected void paint(Graphics graphics) {
		int rowHeight = this.bitmap.getHeight();
		int fontHeight = Font.getDefault().getHeight();
		int fontWidth = Font.getDefault().getAdvance(text);
		
		Bitmap icon = this.bitmap;

		int imageHeight = icon.getHeight();

		int fontOffset = (rowHeight - fontHeight) / 2;
		int imageOffset = (rowHeight - imageHeight) / 2;

		graphics.drawText(this.text, fontOffset + icon.getWidth() + fontOffset, fontOffset, Graphics.ELLIPSIS, Display.getWidth()
				- (imageOffset + icon.getWidth() + imageOffset));
		
		graphics.drawBitmap(fontWidth+fontHeight*2, imageOffset, icon.getWidth(), icon.getHeight(), icon, 0, 0);
	}		
}
